const db = require('../../../libs/db.config');
const bcrypt = require("bcryptjs");

module.exports = (data) => {
    const { username, password, phone } = data;
    const searchKey = username ? 'userName' : 'phone';
    const searchValue = username ? username : phone;
    const searchSQL = `SELECT * FROM user WHERE ${searchKey} = "${searchValue}"`;

    return new Promise((resolve, reject) => {
        db.query(searchSQL, async function (err, result) {
            if (err) {
                reject(err); // 查询出错，返回错误给调用方处理
            } else {
                if (result.length > 0 && bcrypt.compareSync(password, result[0].password)) {
                    resolve(true); // 匹配成功，返回 true
                } else {
                    resolve(false); // 匹配失败，返回 false
                }
            }
        });
    });
};